package com.zgh.myexamproject;


import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.zgh.myexamproject.bean.User;
import com.zgh.myexamproject.util.MyDBHelper;
import com.zgh.myexamproject.util.loginInfo;


public class MyIFActivity extends AppCompatActivity {

    TextView et_nc,et_phone,et_bz;
    RadioButton et_male,et_female;
    RadioGroup et_group;
    private MyDBHelper myDBHelper;
    private SQLiteDatabase db;
    String sex = "男";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.personxx);
        myDBHelper = new MyDBHelper(this);
        et_nc = findViewById(R.id.et_nc);
        et_phone = findViewById(R.id.et_dh);
        et_bz = findViewById(R.id.et_bz);
        et_male = findViewById(R.id.radioMale);
        et_female = findViewById(R.id.radioFemale);
        et_group = findViewById(R.id.radioGroupSex);
        /*db.execSQL("update tb_User set nickname='coach',phone='13020900078',note='5.28下午' where username = ?",new String[]{loginInfo.getUsername()});*/
       /* db.execSQL("delete from tb_User where username = ?",new String[]{loginInfo.getUsername()});*/
        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        if(bundle==null){
            getUser(loginInfo.getUsername());
        }
        else{
            String gamer = bundle.getString("gamer");
            getUser(gamer);
        }
        Button bt_add = findViewById(R.id.btn_add);
        bt_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                updateUser();
            }
        });
    }
    public void getUser(String name){
        db = myDBHelper.getReadableDatabase();
        /*db.execSQL("update tb_User set nickname='coach',phone='13020900078',note='5.28下午' where username = ?",new String[]{loginInfo.getUsername()});*/
        /* db.execSQL("delete from tb_User where username = ?",new String[]{loginInfo.getUsername()});*/
        Cursor cursor = db.rawQuery("select * from tb_User where username = ?", new String[]{name});
        int resultCounts = cursor.getCount();
        if(cursor.getCount()!=0) {
            cursor.moveToFirst();
            et_nc.setText(cursor.getString(cursor.getColumnIndex("nickname")));
            et_phone.setText(cursor.getString(cursor.getColumnIndex("phone")));
            et_bz.setText(cursor.getString(cursor.getColumnIndex("note")));
            sex = cursor.getString(cursor.getColumnIndex("sex"));
            if (sex!=null && sex.equals("男")){
                et_group.check(R.id.radioMale);
            }else if(sex!=null && sex.equals("女")){
                et_group.check(R.id.radioFemale);
            }
        }
    }
    public void updateUser(){
        String sex;
        if(et_male.isChecked()){
            sex = "男";
        }else{
            sex = "女";
        }
        ContentValues value = new ContentValues();
        value.put("nickname", et_nc.getText().toString().trim());
        value.put("sex",sex);
        value.put("phone", et_phone.getText().toString().trim());
        value.put("note", et_bz.getText().toString().trim());
        db = myDBHelper.getWritableDatabase();
        int result = db.update("tb_User",value,"username=?",new String[]{loginInfo.getUsername()});
        if(result>0) {
            Toast.makeText(this, "修改成功", Toast.LENGTH_SHORT).show();
        }else{
            Toast.makeText(this, "修改失败", Toast.LENGTH_SHORT).show();
        }
        db.close();
    }
}
